using System;
using System.Collections.Generic;
using System.Text;

namespace Noein.GeneticAlgorithm.New
{
    public abstract class Selector
    {
        public abstract List<Individual> Select(List<Individual> pop, int count);
    }

    // TODO fitness proportional selection

    public class TopFit : Selector
    {
        public override List<Individual> Select(List<Individual> pop, int count)
        {
            // sort population by fitness
            pop.Sort(
                delegate(Individual x, Individual y)
                {
                    float f = y.Fitness - x.Fitness;
                    if (f < 0)
                        return -1;
                    else if (f > 0)
                        return 1;
                    else
                        return 0;
                });

            // return most fit individuals
            return pop.GetRange(0, count);
        }
    }

    public class RandomSelector : Selector
    {
        private Random rand = new Random();

        public override List<Individual> Select(List<Individual> pop, int count)
        {
            List<Individual> retval = new List<Individual>(pop);
            while (retval.Count > count)
            {
                retval.RemoveAt(rand.Next(retval.Count));
            }
            return retval;
        }
    }
}
